<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <ul id="container"></ul>

    <script>
      let data = {
        Fish: {
          trout: {},
          salmon: {},
        },

        Tree: {
          Huge: {
            sequoia: {},
            oak: {},
          },
          Flowering: {
            "apple tree": {},
            magnolia: {},
          },
        },
        test: {},
      };
      let container = document.getElementById("container");
      container.innerHTML = getHTMLString(data);

      function getHTMLString(data, createUl = false) {
        let str = "";
        if (createUl) str += "<ul>";
        const keys = Object.keys(data);

        keys.forEach((key) => {
          const values = Object.keys(data[key]);
          str += `<li>${key}</li>`;
          if (values.length > 0) {
            str += getHTMLString(data[key], true);
          }
        });
        if (createUl) str += "</ul>";
        console.log(str);
        return str;
      }
    </script>
  </body>
</html>
